﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Script.Serialization;
using VQWEB.administrator.sources;
using VQData;
using VQWEB.common;
using System.Reflection;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for DiemHandler
    /// </summary>
    public class hn_DiemHandler : IHttpHandler, IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Diem
            string add_diem = context.Request["add_diem"];
            if (!string.IsNullOrEmpty(add_diem))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                var obj = bussiness.InsertOrUpdate<hn_diem>(add_diem);
                if (obj.nganh_array_id != null)
                    bussiness.InsertAllInOne<hn_diem_nganh>(obj.nganh_array_id, "nganh_id", "diem_id", obj.diem_id);
                if (obj.truong_array_id != null)
                    bussiness.InsertAllInOne<hn_diem_truong>(obj.truong_array_id, "truong_id", "diem_id", obj.diem_id);
                context.Response.Write("Thực thi thành công !");
            }
            string view_diem = context.Request["view_diem"];
            if (!string.IsNullOrEmpty(view_diem))
            {
                string Id = context.Request["Id"];
                if (!string.IsNullOrEmpty(Id))
                    RenderSingle(context, Convert.ToInt32(Id));
                else
                    RenderTruongNganh(context);

            }

            string list_diem= context.Request["list_diem"];
            if (!string.IsNullOrEmpty(list_diem))
            {
                RenderList(context);
            }
            
            string delete_diem = context.Request["delete_diem"];
            if (!string.IsNullOrEmpty(delete_diem))
            {
                 BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                 bussiness.Delete<hn_diem>(delete_diem);
                RenderList(context);
            }

            string lockunlock_diem = context.Request["lockunlock_diem"];
            if (!string.IsNullOrEmpty(lockunlock_diem))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.LockUnLock<hn_diem>(lockunlock_diem, "diem_active");
                RenderList(context);
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private void RenderTruongNganh(HttpContext context) {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            var truong = bussiness.RenderArrayCheckboxJSON(HuongNghiepData.ViewTruong(),null, "id", "ten", "truong_array_id[]");
            var nganh = bussiness.RenderArrayCheckboxJSON(HuongNghiepData.ViewNganh(),null, "id", "ten", "nganh_array_id[]");

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("truong",truong);
            dic.Add("nganh", nganh);

            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }

        private void RenderSingle(HttpContext context, int Id)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            object obj = bussiness.Get<hn_diem>(
               d => d.diem_id.Equals(Id),
               d => new
               {
                   id = d.diem_id,
                   nam = d.diem_nam,
                   chuan = d.diem_chuan,
                   nv1 = d.diem_nv1,
                   nv2 = d.diem_nv2,
                   nv3 = d.diem_nv3,
                   active = d.diem_active
               });

            PropertyInfo info = obj.GetType().GetProperty("id");
            var value = info.GetValue(obj, null);

            List<object> array_nganh_id = bussiness.List<hn_diem_nganh>(
                null,
                null,
                n => n.diem_id.Equals(value),
                null,
                n => n.nganh_id
                ).ToList();
            List<object> array_truong_id = null;

            array_truong_id = bussiness.List<hn_diem_truong>(
               null,
               null,
               n => n.diem_id.Equals(value),
               null,
               n => n.truong_id
               ).ToList();
            List<Dictionary<string, object>> listtruong = bussiness.RenderArrayCheckboxJSON(HuongNghiepData.ViewTruong(), array_truong_id, "id", "ten", "truong_array_id[]");
            List<Dictionary<string, object>> listnganh = bussiness.RenderArrayCheckboxJSON(HuongNghiepData.ViewNganh(), array_nganh_id, "id", "ten", "nganh_array_id[]");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("diem", obj);
            dic.Add("truong", listtruong);
            dic.Add("nganh", listnganh);
            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);

        }
        
        private void RenderList(HttpContext context)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            IList<object> list = bussiness.List<hn_diem>(
                null,
                null,
                null,
                null,
                d => new
                {
                    id = d.diem_id,
                    nam = d.diem_nam,
                    chuan = d.diem_chuan,
                    nv1 = d.diem_nv1,
                    nv2 = d.diem_nv2,
                    nv3 = d.diem_nv3,
                    active = d.diem_active
                });

            
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);

        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}